package com.sunmouse.toolkit.fragment.chart;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sunmouse.toolkit.R;
import com.sunmouse.toolkit.fragment.BaseFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.view.ColumnChartView;

/**
 * A simple {@link Fragment} subclass.
 */
public class ColumnFragment extends BaseFragment {


    @BindView(R.id.column_chart)
    ColumnChartView columnChart;
    Unbinder unbinder;

    public ColumnFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_column, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void init() {
        ArrayList<Column> columns = generateColumn();
        ColumnChartData columnData = generateColumnData(columns);
        this.columnChart.setColumnChartData(columnData);
    }

    /**
     * generate column mData
     *
     * @param columns
     * @return
     */
    private ColumnChartData generateColumnData(ArrayList<Column> columns) {
        ColumnChartData columnData=new ColumnChartData(columns);
        columnData.setAxisXBottom(generateXAxis());
        columnData.setAxisYLeft(generateYAxis());
        return columnData;
    }

    /**
     * generate columns
     *
     * @return
     */
    private ArrayList<Column> generateColumn() {
        ArrayList<Column> columns = new ArrayList<>();
        ArrayList<SubcolumnValue> subColumns;
        for (int i = 0; i < 10; i++) {
            subColumns = new ArrayList<>();
            subColumns.add(new SubcolumnValue((float) (50 * Math.random()), getCircleColor()));
            columns.add(new Column(subColumns));
        }
        return columns;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
